package com.epam.tags;

import java.io.IOException;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Logger;

import com.epam.dao.impl.UserDAO;

public class HeaderTagHandler extends TagSupport {

	private static Logger LOG = Logger.getLogger(HeaderTagHandler.class);
	private static final long serialVersionUID = 1L;

	private String login;

	public int doStartTag() {
		JspWriter out = pageContext.getOut();
		UserDAO userdao = new UserDAO();
		if (!login.equals(null)) {
			try {
				out.println("<header>");
				out.println("<table>");
				if (userdao.findByLogin(login).getRole().toString()
						.equals("ADMIN")) {
					out.println("<th><form action='controller' method='get'><input type='hidden' name='address' value='ADMIN_PAGE'>"
							+ "<input type='hidden' name='action' value='goToPage'>"
							+ "<input type='submit' value='Admin Page'></form></th>");
				} else 
				if (userdao.findByLogin(login).getRole().toString()
						.equals("USER")) {
					out.println("<th><form action='controller' method='get'><input type='hidden' name='action' value='loadPurchases'><input type='submit' value='My purchases'></th></form>");
					out.println("<form action='controller' method='get'><input type='hidden' name='address' value='SHOPPING_CART'>"
							+ "<input type='hidden' name='action' value='goToPage'>"
							+ "<th><input type='submit' value='Shopping Cart'></form></th>");
					out.println("<th><form action='controller' method='get'>"
							+ "<input type='hidden' name='action' value='loadAllVouchers'>"
							+ "<input type='hidden' name='address' value='TOUR_LIST'>"
							+ "<input type='hidden' name='pageNumber' value='1'>"
							+ "<input type='submit' value='Tour List'></form></th>");
				}
				
				out.println("<th><form action='controller' method='get'><input type='hidden' name='action' value='signOut'><input type='submit' value='Sign out'></form></th>");
				out.println("</table>");
				out.println("</header>");
				out.println("<p>");
			
			} catch (IOException e) {
				LOG.error(e);
			}
		}
		return SKIP_BODY;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

}
